package com.hd123.rumba.gwt.widget.client.grid;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.hd123.rumba.gwt.widget.client.Util;

public class DxNavigator extends Composite implements ClickListener {
  HorizontalPanel hp;
  Label info;
  Button first, prior, next, last;
  final DxGrid grid;
  final DxGridDataProvider provider;
  AsyncCallback callback;

  public DxNavigator(final DxGridDataProvider provider, final DxGrid grid) {
    this.provider = provider;
    this.grid = grid;

    info = new Label();

    first = new Button("首页");
    first.addClickListener(this);
    prior = new Button("上页");
    prior.addClickListener(this);
    next = new Button("下页");
    next.addClickListener(this);
    last = new Button("末页");
    last.addClickListener(this);
    HorizontalPanel buttons = new HorizontalPanel();
    buttons.add(first);
    buttons.add(prior);
    buttons.add(next);
    buttons.add(last);

    hp = new HorizontalPanel();
    hp.setStyleName("DxNavigator");
    hp.add(info);
    hp.setCellHorizontalAlignment(info, HorizontalPanel.ALIGN_LEFT);
    hp.setCellVerticalAlignment(info, HorizontalPanel.ALIGN_MIDDLE);
    hp.add(buttons);
    hp.setCellHorizontalAlignment(buttons, HorizontalPanel.ALIGN_RIGHT);

    callback = new AsyncCallback() {
      public void onFailure(Throwable caught) {
        Util.showErrorDialog(caught.getMessage());
      }

      public void onSuccess(Object result) {
        // Window.alert("DxNavigator.callback.onSuccess " +
        // System.currentTimeMillis() +
        // " " + result.toString());
        if (((Boolean) result).booleanValue())
          grid.refresh();
        setStatus();
      }
    };

    initWidget(hp);
    setWidth("100%");
  }

  public void onClick(Widget sender) {
    if (sender == first) {
      if (provider.getCurrentPage() > 0) {
        gotoPage(0);
      }
    } else if (sender == prior) {
      if (provider.getCurrentPage() > 0) {
        gotoPage(provider.getCurrentPage() - 1);
      }
    } else if (sender == next) {
      if (provider.getCurrentPage() < provider.getPageCount() - 1) {
        gotoPage(provider.getCurrentPage() + 1);
      }
    } else if (sender == last) {
      if (provider.getCurrentPage() < provider.getPageCount() - 1) {
        gotoPage(provider.getPageCount() - 1);
      }
    }
  }

  public void setStatus() {
    String s;
    if (provider.getTotalCount() == 0) {
      s = "记录:" + 0 + "-" + 0 + "/" + 0 + " 页:" + 0 + "/" + 0;
    } else {
      int start = provider.getCurrentPage() * provider.getPageSize() + 1;
      int end = start + provider.getPageSize() - 1;
      if (end > provider.getTotalCount())
        end = provider.getTotalCount();
      s = "记录:" + start + "-" + end + "/" + provider.getTotalCount() + " 页:"
          + (provider.getCurrentPage() + 1) + "/" + provider.getPageCount();
    }
    info.setText(s);
    info.setStyleName("DxNavigator-Info");

    first.setEnabled(provider.getCurrentPage() > 0);
    prior.setEnabled(provider.getCurrentPage() > 0);
    next.setEnabled(provider.getCurrentPage() < provider.getPageCount() - 1);
    last.setEnabled(provider.getCurrentPage() < provider.getPageCount() - 1);
    boolean visible = provider.getPageCount() > 1;
    first.setVisible(visible);
    prior.setVisible(visible);
    next.setVisible(visible);
    last.setVisible(visible);
  }

  public void gotoPage(int page) {
    info.setText("请稍候...");
    info.setStyleName("DxNavigator-Info-Progress");
    first.setEnabled(false);
    prior.setEnabled(false);
    next.setEnabled(false);
    last.setEnabled(false);
    provider.fetch(page, callback);
  }
}
